package com.molyfun.parents.activity.coursecenter.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.ProductCommentListResponse;
import com.molyfun.parents.utils.StringUtils;
import com.squareup.picasso.Picasso;
import com.willy.ratingbar.ScaleRatingBar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.carbs.android.expandabletextview.library.ExpandableTextView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ProductCommentCellView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.avator)
    CircleImageView mAvator;
    @InjectView(R.id.name)
    TextView mName;
    @InjectView(R.id.name_time)
    TextView mNameTime;
    @InjectView(R.id.simpleRatingBar)
    ScaleRatingBar mScaleRatingBar;
    @InjectView(R.id.content)
    ExpandableTextView mContent;

    @InjectView(R.id.line)
    View mLine;

    public ProductCommentCellView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ProductCommentCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProductCommentCellView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_product_comment_cell, this);
        ButterKnife.inject(this);

    }

    public void initData(ProductCommentListResponse.ProductCommentBean productCommentBean){
        initData(productCommentBean, false);
    }

    /**
     * init view
     *
     */
    public void initData(ProductCommentListResponse.ProductCommentBean productCommentBean, boolean isLast) {
        mContent.updateForRecyclerView(productCommentBean.getContent(), mContent.getWidth(), 0);

        if (!TextUtils.isEmpty(productCommentBean.getUserimg())) {
            Picasso.with(getContext())
                    .load(productCommentBean.getUserimg())
                    .config(Bitmap.Config.RGB_565)
                    .placeholder(R.mipmap.ic_header_default)
                    .into(mAvator);
        } else {
            mAvator.setImageResource(R.mipmap.ic_header_default);
        }
        mName.setText(productCommentBean.getCommentusername());
        mNameTime.setText(StringUtils.formatDate(productCommentBean.getCreatedatetime()));
        mScaleRatingBar.setRating(Float.parseFloat(productCommentBean.getDegree()));
        mScaleRatingBar.setClickable(false);
//        mScaleRatingBar.set

        if(isLast){
            mLine.setVisibility(View.GONE);
        }else{
            mLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

}